<template>
<div>

  <el-table
      :data="tableData"
      border
      height="600"
      :summary-method="getSummaries"
      show-summary
      style="width: 100%">
    <el-table-column
        prop="id"
        label="ID"
        width="180">
    </el-table-column>
    <el-table-column
        prop="userId"
        label="用户id">
    </el-table-column>


    <el-table-column
        label="成本">
      <template slot-scope="scope">
        ￥{{scope.row.goodsList.goodsNum*scope.row.goodsList.inPrice}}
      </template>
    </el-table-column>
    <el-table-column
        prop="price"
        label="销售额">
    </el-table-column>
    <el-table-column
        label="利润">
      <template slot-scope="scope2">
        {{scope2.row.price-scope2.row.goodsList.goodsNum*scope2.row.goodsList.inPrice}}
      </template>
    </el-table-column>
  </el-table>




</div>
</template>

<script>
import axios from "axios";

export default {
  name: "dataAnalysis",
  data(){
    return{
      tableData:[
        {
          id: "4",
          goodsId: null,
          userId: "1",
          orderNum: 2,
          orderDate: "2022-1-1",
          price: 180.0,
          delivery: "未发货",
          user: {
            id: "1",
            password: "12345",
            status: true,
            name: "罗慧"
          },
          goods: null,
          goodsList:
            {
              id: "1",
              goodsName: "毛衣",
              goodsNum: 2,
              goodsType: "1",
              inPrice: 80.0,
              outPrice: 180.0,
              description: null,
              status: "未发货"
            }
        },
        {
          id: "4",
          goodsId: null,
          userId: "1",
          orderNum: 2,
          orderDate: "2022-1-1",
          price: 180.0,
          delivery: "未发货",
          user: {
            id: "1",
            password: "12345",
            status: true,
            name: "罗慧"
          },
          goods: null,
          goodsList:
            {
              id: "1",
              goodsName: "毛衣",
              goodsNum: 2,
              goodsType: "1",
              inPrice: 80.0,
              outPrice: 180.0,
              description: null,
              status: "未发货"
            }

        }
        , {
          id: "4",
          goodsId: null,
          userId: "1",
          orderNum: 2,
          orderDate: "2022-1-1",
          price: 180.0,
          delivery: "未发货",
          user: {
            id: "1",
            password: "12345",
            status: true,
            name: "罗慧"
          },
          goods: null,
          goodsList:
            {
              id: "1",
              goodsName: "毛衣",
              goodsNum: 2,
              goodsType: "1",
              inPrice: 80.0,
              outPrice: 180.0,
              description: null,
              status: "未发货"
            }

        }
      ]
    }
  },
  mounted(){
    // if(window.$x.name != undefined){
    //   axios.get(`http://localhost:8088/user`,{
    //     params:{
    //       name:window.$x.name
    //     }
    //   }).then(response=>{
    //     if(response.data){
    //       //console.log(this.GLOBAL)
    //       this.GLOBAL.user = response.data
    //       //console.log(this.GLOBAL)
    //     }
    //   })
    // }else{
    //   this.$message({
    //     type:'error',
    //     message:'请登录'

    //   })
    //   console.log('错浏览')
    // }
  },

  methods:{
    getSummaries(param) {
      console.log(param);
      console.log(this);
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '销售额';
          return;
        }
        if (index === 1) {
          sums[index] = '';
          return;
        }
        if (index === 2) {
          sums[index] = '';
          return;
        }
        if (index === 4) {
          sums[index] = '';
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] += ' 元';
        } else {
          sums[index] = 'N/A';
        }
      });

      return sums;
    }

  }
}
</script>

<style scoped>

</style>